﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using MeSign.Reader.Helper;
using System;
using System.Threading.Tasks;

namespace MeSign.Reader.Controls
{
    public class WindowHeader : UserControl
    {
        public static readonly StyledProperty<IBitmap> IconProperty = AvaloniaProperty.Register<WindowHeader, IBitmap>(nameof(Icon));

        public IBitmap Icon
        {
            get { return GetValue(IconProperty); }
            set { SetValue(IconProperty, value); ShowIcon(); }
        }

        private int iconWidth = 20;

        public int IconWidth
        {
            get { return iconWidth; }
            set { iconWidth = value; ShowIcon(); }
        }

        public static StyledProperty<bool> ShowMinProperty = AvaloniaProperty.Register<WindowHeader, bool>(nameof(ShowMin));
        public bool ShowMin
        {
            get { return GetValue(ShowMinProperty); }
            set { SetValue(ShowMinProperty, value); }
        }

        public static StyledProperty<bool> ShowMaxProperty = AvaloniaProperty.Register<WindowHeader, bool>(nameof(ShowMax));
        public bool ShowMax
        {
            get { return GetValue(ShowMaxProperty); }
            set { SetValue(ShowMaxProperty, value); }
        }

        public WindowHeader()
        {
            this.InitializeComponent();
        }

        public event Func<object, RoutedEventArgs, Task> CloseTapped;

        private Image _icon;
        private Grid _gridHeader;
        private Grid _gridBtns;
        private Grid _gridMax;
        private Button _btnMin;
        private Button _btnMaxToMax;
        private Button _btnMaxToMin;
        private Button _btnClose;
        private Window _parent;

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);

            _gridHeader = this.FindControl<Grid>("gridHeader");
            _gridBtns = this.FindControl<Grid>("gridBtns");
            _gridMax = this.FindControl<Grid>("gridMax");
            _btnMin = this.FindControl<ImageButton>("btnMin");
            _btnMaxToMax = this.FindControl<ImageButton>("btnMaxToMax");
            _btnMaxToMin = this.FindControl<ImageButton>("btnMaxToMin");
            _btnClose = this.FindControl<ImageButton>("btnClose");
            _icon = this.FindControl<Image>("icon");

            _gridHeader.PointerPressed += _gridHeader_PointerPressed;
            _gridHeader.DoubleTapped += _gridHeader_DoubleTapped;

            _gridBtns.PointerPressed += _gridBtns_PointerPressed;
            _gridBtns.DoubleTapped += _gridBtns_DoubleTapped;

            _btnMin.Click += _btnMin_Click;
            _btnMaxToMax.Click += _btnMaxToMax_Click;
            _btnMaxToMin.Click += _btnMaxToMin_Click;
            _btnClose.Click += _btnClose_Click;

            this.Initialized += WindowHeader_Initialized;
        }

        /// <summary>
        /// 属性变更事件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="change"></param>
        protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
        {
            base.OnPropertyChanged(change);
            if (change.Property == ShowMinProperty)
            {
                if (ShowMin)
                {
                    _btnMin.IsVisible = true;
                }
                else
                {
                    _btnMin.IsVisible = false;
                }
            }
            if (change.Property == ShowMaxProperty)
            {
                if (ShowMax)
                {
                    _gridMax.IsVisible = true;
                    _btnMaxToMax.IsVisible = true;
                }
                else
                {
                    _gridMax.IsVisible = false;
                    _btnMaxToMax.IsVisible = false;
                    _btnMaxToMin.IsVisible = false;
                }
            }
        }

        private void WindowHeader_Initialized(object sender, EventArgs e)
        {
            _parent = VisualHelper.FindParent<Window>(this);
            if (!ShowMax)
            {
                this.PointerMoved += WindowHeader_PointerMoved;
            }
        }

        private void WindowHeader_PointerMoved(object sender, Avalonia.Input.PointerEventArgs e)
        {
            if (!ShowMax && _parent.WindowState == WindowState.Maximized)
            {
                _parent.WindowState = WindowState.Normal;
                _btnMaxToMax.IsVisible = true;
                _btnMaxToMin.IsVisible = false;
            }
        }

        private async void _btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (CloseTapped != null)
            {
                await CloseTapped.Invoke(sender, e);
            }
            if (!e.Handled)
            {
                _parent.Close();
            }
            e.Handled = true;
        }

        private void _btnMaxToMin_Click(object sender, RoutedEventArgs e)
        {
            ChangeWindowSize();
        }

        private void _btnMaxToMax_Click(object sender, RoutedEventArgs e)
        {
            ChangeWindowSize();
        }

        private void _btnMin_Click(object sender, RoutedEventArgs e)
        {
            _parent.WindowState = WindowState.Minimized;
        }

        private void _gridBtns_DoubleTapped(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
        }

        private void _gridBtns_PointerPressed(object sender, Avalonia.Input.PointerPressedEventArgs e)
        {
            e.Handled = true;
        }

        private void _gridHeader_DoubleTapped(object sender, RoutedEventArgs e)
        {
            if (ShowMax)
            {
                ChangeWindowSize();
            }
        }

        private void ChangeWindowSize()
        {
            if (_parent.WindowState == WindowState.Maximized)
            {
                _parent.WindowState = WindowState.Normal;
                _btnMaxToMax.IsVisible = true;
                _btnMaxToMin.IsVisible = false;
            }
            else if (_parent.WindowState == WindowState.Normal)
            {
                _parent.WindowState = WindowState.Maximized;
                _btnMaxToMax.IsVisible = false;
                _btnMaxToMin.IsVisible = true;
            }
        }

        private void _gridHeader_PointerPressed(object sender, Avalonia.Input.PointerPressedEventArgs e)
        {
            _parent.BeginMoveDrag(e);
        }

        private void ShowIcon()
        {
            _icon.Source = Icon;
            if (Icon != null)
            {
                _icon.Width = IconWidth;
            }
            else
            {
                _icon.Width = 0;
            }
        }
    }
}
